<?php

class Session extends CI_Controller {
    
    public function Session() {
        parent::__construct();
        
        $this->load->library('session');
    }

    public function index() {
        $this->load->view('layout/login');
    }

    public function authenticate() {
        $this->load->model('Member_model', '', true);

        if($this->Member_model->member_admin_authenticate($this->input->post('username'), $this->input->post('password'))) {
            redirect('/router/panel','refresh');
        } else {
            $login = $this->Member_model->authenticate($this->input->post('username'), $this->input->post('password'));
            if ($login['authentification']) {
                $this->session->set_userdata('member_id', $login['data']['member_id']);
                $this->session->set_userdata('firstname', $login['data']['firstname']);
                $this->session->set_userdata('logged', true);
                redirect('/router/view_users','refresh');
            }
            else {
                redirect('/login/','refresh');
            }
        }
        exit();
    }

    public function logout() {
        $this->session->unset_userdata('member_id');
        $this->session->unset_userdata('firstname');
        $this->session->unset_userdata('logged');
        redirect('','refresh');
        
        exit();
    }
}

?>
